package csel.view.status;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.LinkedList;

import javax.swing.*;

import csel.model.*;
import csel.model.items.*;
import csel.model.entity.Character;

public class EquipmentMenu extends JPanel implements Observer<Object>
{

	private static final long serialVersionUID = 1L;
	
	private Character myCharacter;
	private ActionListener myListener;
	private final EquipMenuManager myManager;
	
	private JScrollPane myScrollPane;
	private JPanel equipPanel;
	
	private Collection<EquippableItem> equipSet;
	private LinkedList<JLabel> nameSet = new LinkedList<JLabel>();
	private LinkedList<JLabel> descSet = new LinkedList<JLabel>();
	private LinkedList<JButton> uneqButtonSet = new LinkedList<JButton>();
	
	private EquippableItem lastSelected = null;
	
	public static EquipmentMenu initialize(Character myCharacter, ActionListener myListener)
	{
		EquipmentMenu theEquipMenu = new EquipmentMenu(myCharacter,myListener);
		myCharacter.addObserver(theEquipMenu);
		return theEquipMenu;
	}
	
	private EquipmentMenu(Character myCharacter, ActionListener myListener)
	{
		this.myCharacter = myCharacter;
		this.myListener = myListener;
		myManager = new EquipMenuManager();
		
		equipPanel = new JPanel();
		
		setLayout(new GridBagLayout());
		equipPanel.setLayout(new GridBagLayout());
		
		buildEquipPanel();
		
	}
	
	private void buildEquipPanel()
	{
		GridBagConstraints c = new GridBagConstraints();
		
		nameSet = new LinkedList<JLabel>();
		descSet = new LinkedList<JLabel>();
		uneqButtonSet = new LinkedList<JButton>();
		
		equipSet = myCharacter.getEquipment().getAllEquippedItems().values();
		int i = 3*equipSet.size();
		
		for (EquippableItem e: equipSet)
		{
			nameSet.add(textLabel(e.getName(),Color.BLACK,12));
			descSet.add(textLabel(e.getDescription(),Color.BLUE,12));
			uneqButtonSet.add(unequipButton(e));
		}
		
		for (int a = 0; a < i; a += 3)
		{
			c.gridwidth = 1;
			c.gridy = a;
			c.ipady = 5;
			c.gridx = 0;
			equipPanel.add(nameSet.get(a/3),c);
			c.gridy = a+1;
			equipPanel.add(descSet.get(a/3),c);
			c.gridy = a+2;
			equipPanel.add(uneqButtonSet.get(a/3),c);
		}

		myScrollPane = new JScrollPane(equipPanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		myScrollPane.setPreferredSize(new Dimension(375,125));

		c.gridx = 0;
		c.gridy = 1;
		add(myScrollPane,c);

		setPreferredSize(new Dimension(375,125));
	}
	
	private JButton unequipButton(EquippableItem myEquip)
	{
		JButton myButton = new JButton("Unequip");
		myButton.setActionCommand("emUnequip");
		myButton.putClientProperty("contents",myEquip);
		myButton.addActionListener(myManager);
		return myButton;
	}
	
	public EquippableItem getSelectedEquipment()
	{
		//TODO: implement me
		return lastSelected;
	}
	
	public void mupdate(Object o, Object param)
	{
		if (o.equals(myCharacter))
		{
			//TODO: implement me!
		}
	}
	
	public void setCharacter(Character myCharacter)
	{
		this.myCharacter = myCharacter;
		myCharacter.addObserver(this);
	}
	
	private JLabel textLabel(String s, Color c, int size)
	{
		JLabel myLabel = new JLabel(s);
		myLabel.setFont(new Font("Serif", Font.PLAIN, size));
		myLabel.setForeground(c);
		return myLabel;

	}
	
	//only responsible for enabling/disabling the use and equip buttons based on the type
    //of item that is selected.
    private class EquipMenuManager implements ActionListener
    {
            public void actionPerformed(ActionEvent e)
            {
            	JButton theButton = ((JButton) e.getSource());
                lastSelected = (EquippableItem) theButton.getClientProperty("contents");
                System.out.println("lastSelected = "+lastSelected.getName());
                myListener.actionPerformed(e);
            }
    }
	
    private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException
    {
            throw new java.io.NotSerializableException("EquipmentViewer should not be serialized.");
    }
    
    private void readObject(java.io.ObjectInputStream in) throws java.io.IOException
    {
            throw new java.io.NotSerializableException("EquipmentViewer should not be serialized.");
    }

    
}
